﻿using EnvDTE;
using EnvDTE80;
using Expect.Core;
using Microsoft.VisualStudio.Shell;

namespace Expect.VisualStudio.Extensions.Commands.Handlers
{
    class CreateContainerMethodWithFixtureData : UnresolvedFixtureCommand
    {
        public CreateContainerMethodWithFixtureData(IExpectPackage pkg)
            : base(pkg, CommandIDs.cmdidCreateContainerMethodWithFixtureData)
        {
        }

        public override string DisplayText
        {
            get
            {
                return "Container => create method with fixture data";
            }
        }

        public override bool ShowCommand(SelectionResolution resolution)
        {
            if (
                base.ShowCommand(resolution) &&
                resolution.IsFixture &&
                resolution.Fixture != null &&
                resolution.Fixture.Rows.Count > 2 &&
                resolution.Fixture.RowsHaveSameNumberOfCells(1)
                )
            {
                return true;
            }

            return false;
        }

        public override void ExecuteCommand(SelectionResolution resolution)
        {
            var dte = (DTE2)GetVSService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));

            var container = CreateContainerMethod.GetContainerCodeClass(resolution, dte);

            CodeFunction2 method = CreateContainerMethod.GetFixtureMethod(resolution, resolution.Fixture.Rows[0], container);
            method.AddParameter("data", typeof(FixtureData).FullName, method.Parameters.Count);

            var pt = method.GetStartPoint(vsCMPart.vsCMPartBody);

            var wnd = container.ProjectItem.Open();
            wnd.Activate();

            EditorHelper.SetCursorTo(dte, wnd, pt.Line, 0);
        }
    }
}
